import 'package:flutter/material.dart';

import 'code_screen.dart';

abstract class CommonTabPageState<T> extends State
    with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: getTabs().length, vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('其他辅助组件'),
        bottom: TabBar(
          tabs: getTabs(),
          controller: _tabController,
          isScrollable: true,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Text('查看\n代码'),
        onPressed: _jumpCode,
      ),
      body: TabBarView(
        children: getPages(),
        controller: _tabController,
      ),
    );
  }

  _jumpCode() {
    Navigator.of(context).pushNamed(CodeScreen.routeName,
        arguments: {"file": "${getFileName(_tabController.index)}"});
  }

  List<Tab> getTabs();

  List<Widget> getPages();

  String getFileName(int index);
}
